<?php
      /**
      * 说明书
      * 1：在服务器上安装 LNMP https://lnmp.org/install.html
      * 2：将修改后的本文件命名为 index.php，并复制入虚拟主机默认目录 /home/wwwroot/default
      * 3：删除原目录中的 index.html
      * 4：通过主机 IP 访问该站点
      *
      * Martin
      * 2018-08-20 
      */
      // ** API接口信息 **
      $VEID = 'VEID信息';
      $API_KEY = 'APIKEY信息';
	  
      // 申请API数据
      $request_URL = 'https://api.64clouds.com/v1/getServiceInfo?veid=' . $VEID . '&api_key=' . $API_KEY;
      $serviceInfo = json_decode (file_get_contents ($request_URL));

      // 相关地址、IP信息
      $ip_addresses = $serviceInfo->ip_addresses;												//取IP
      $node_location = $serviceInfo->node_location;												//取服务器地区
      $data_counter = floatval($serviceInfo->data_counter);										//取流量相关数据
      $plan_monthly_data = floatval($serviceInfo->plan_monthly_data);
      $monthly_data_multiplier = floatval($serviceInfo->monthly_data_multiplier);
      $data_next_reset = intval($serviceInfo->data_next_reset) + 24 * 60 * 60;					//取时间相关数据

      // 数据处理
      $ipstr = '';
      foreach($ip_addresses as $key => $value) {
            $ipstr .= $value . ' ';}
      $data_used = round($data_counter * $monthly_data_multiplier / pow(2, 30), 2);				//已用流量
      $data_total = round($plan_monthly_data * $monthly_data_multiplier / pow(2, 30), 2);		//总流量
      $data_average = round(($data_total - $data_used) / (date("d", $data_next_reset - time() - (24 + 8) * 60 * 60) + 1), 2);		//平均每日使用流量
      
	  $data_usage = round($data_used / $data_total * 100, 2);									//使用百分比
	  $data_surplus = round(($data_total - $data_used) / $data_total * 100, 2);					//剩余百分比
	  
      $data_cycle_now = date("Y年m月d日 H时i分s秒", time());											//现在时间
      $data_cycle_reset = date("Y年m月d日 H时i分s秒", $data_next_reset);								//重置时间
	  $data_remaining_time = date("d天 H时i分s秒", $data_next_reset - time() - (24 + 8) * 60 * 60);	//倒计时
	  
	  //网页自动刷新
	  echo ("<script type=\"text/javascript\">");
	  echo ("function fresh_page()"); 
	  echo ("{");
	  echo ("window.location.reload();");
	  echo ("}"); 
	  echo ("setTimeout('fresh_page()',10000);"); 
	  echo ("</script>");
?>
<!DOCTYPE html>
<html lang="en">
<head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>VPS流量数据统计</title><!-- 网站目录名 -->
      <meta charset="UTF-8">
      <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
            crossorigin="anonymous">
      <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
            crossorigin="anonymous"></script>
</head> 
<body>
      <div class="container">
			<!-- 标题 -->
            <div class="page-header">
                  <h1> VPS流量数据统计</h1>
            </div>
            <div class="panel panel-default">
                  <!-- 表格名称 -->
				  <div class="panel-heading">
                        <h2 class="panel-title">流量信息——【本页面每十秒自动刷新】</h2>
                  </div>
                  <div class="panel-body">
                        <h2><small><p>IP：<?php echo $ipstr;?></p><p>服务器地址：<?php echo $node_location;  ?></p></small></H2><br>
						<table class="table table-default">
                              <!-- 表格开始信息，包括IP、VPN实际地区 -->
							  <thead>
                                    <!-- 列表表头 -->
                                    <tr>
                                          <th>类型</th>
                                          <th>内容</th>
                                    </tr>
                              </thead>
                              <tbody>
                                    <!-- 流量使用情况 -->
                                    <tr>
                                          <td rowspan="2">流量使用</td>
                                          <td><?php echo '【'.$data_used . ' GiB】 / 【' . $data_total . ' GiB】'; ?></td>
                                    </tr>
                                    <tr>
                                          <td>
                                                <div class="progress">
                                                      <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="<?php echo $data_usage;?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $data_usage;?>%;">
                                                            <span class="sr-only"><?php echo $data_usage . '%';?> </span>
                                                            <?php if($data_usage >= 50) echo $data_usage . ' %';?>
                                                      </div>
                                                      <?php if($data_usage < 50) echo $data_usage . ' %';?>
                                                </div>
                                          </td>
                                    </tr>
									<!-- 流量剩余情况 -->
                                    <tr>
                                          <td rowspan="2">流量剩余</td>
                                          <td><?php echo '【'.($data_total - $data_used). ' GiB】' ; ?></td>
                                    </tr>
                                    <tr>
                                          <td>
                                                <div class="progress">
                                                      <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="<?php echo $data_surplus;?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $data_surplus;?>%;">
                                                            <span class="sr-only"><?php echo $data_surplus . '%';?> </span>
                                                            <?php if($data_surplus >= 50) echo $data_surplus . ' %';?>
                                                      </div>
                                                      <?php if($data_surplus < 50) echo $data_surplus . ' %';?>
                                                </div>
                                          </td>
                                    </tr>
                                    <!-- 平均每天使用量 / 距流量重置还有 -->
                                    <tr>
                                        <td rowspan="2">平均每天使用量 / 距流量重置还有</td>
                                        <td><?php echo  '【'.$data_average.' GiB】 / 【'.$data_remaining_time.'】';?></td>
                                    </tr>
									<tr>
                                    </tr>
									<!-- 当前时间 / 流量重置时间 -->
									<tr>
										<td rowspan="2">当前时间 / 流量重置时间</td>
                                        <td><?php echo '【'.$data_cycle_now.'】 / 【'.$data_cycle_reset.'】';?></td>
									</tr>
									<tr>
                                    </tr>
                              </tbody>
                        </table>
                  </div>
            </div>
      </div>
</body>
</html>